package httpHandler

import (
	"fmt"
	baseHttp "gitee.com/zaiqiang231/go-base-app/base_app/http"
	sessionHandler "gitee.com/zaiqiang231/go-base-session/app/http/handler"
	"gitee.com/zaiqiang231/zcamp-service-user/app/constants"
	"gitee.com/zaiqiang231/zcamp-service-user/app/http/service"
	"gitee.com/zaiqiang231/zcamp-service-user/app/model"
	"gitee.com/zaiqiang231/zcamp-service-user/app/model/req"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Follow(c *gin.Context) {
	var request req.UserFollowReq
	if err := c.ShouldBindQuery(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	uid := c.GetInt64(KEY_AUTH_UID)
	if uid == request.Id {
		baseHttp.WriteResponse(c, http.StatusBadRequest, "", nil)
		return
	}

	relation, err := service.ServiceUserInstance.Follow(uid, request.Id)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}

	baseHttp.WriteResponse(c, 0, "success", gin.H{
		"relation": relation,
	})
}

func UnFollow(c *gin.Context) {
	var request req.UserFollowReq
	if err := c.ShouldBindQuery(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	uid := c.GetInt64(KEY_AUTH_UID)
	if uid == request.Id {
		baseHttp.WriteResponse(c, http.StatusBadRequest, "", nil)
		return
	}

	relation, err := service.ServiceUserInstance.UnFollow(uid, request.Id)
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}

	baseHttp.WriteResponse(c, 0, "success", gin.H{
		"relation": relation,
	})
}

func NumRelations(c *gin.Context) {
	var request req.NumRelationsReq
	if err := c.ShouldBindQuery(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}

	result, err := service.ServiceUserInstance.NumRelations(c, []int64{request.Id})
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}

	baseHttp.WriteResponse(c, 0, "success", result)
}

func Relation(c *gin.Context) {
	var request req.RelationsReq
	if err := c.ShouldBindQuery(&request); err != nil {
		baseHttp.WriteResponse(c, http.StatusBadRequest, err.Error(), nil)
		return
	}
	atom := sessionHandler.GetAtom(c)
	var relation string
	if atom.Uid == request.Id {
		relation = constants.SELF
	} else {
		tmprelation, tmperr := service.ServiceUserInstance.Relation(atom.Uid, request.Id)
		if tmperr != nil {
			baseHttp.WriteResponse(c, http.StatusInternalServerError, tmperr.Error(), nil)
			return
		}
		relation = tmprelation
	}

	baseHttp.WriteResponse(c, 0, "success", relation)
}

func FollowRecommend(c *gin.Context) {
	list, err := service.ServiceUserInstance.FollowRecommend()
	if err != nil {
		baseHttp.WriteResponse(c, http.StatusInternalServerError, err.Error(), nil)
		return
	}
	atom := sessionHandler.GetAtom(c)
	uIDs := []int64{}
	for _, item := range list {
		uIDs = append(uIDs, item.Uid)
	}

	// 用户关系
	relationMap, err := service.ServiceUserInstance.RelationMap(atom.Uid, uIDs)

	var data []model.RecommendFollowUserInfo
	for _, item := range list {
		data = append(data, model.RecommendFollowUserInfo{
			UserInfo: item,
			Relation: relationMap[item.Uid],
			Content:  fmt.Sprintf("%v推荐给你", item.Uid),
		})
	}

	baseHttp.WriteResponse(c, 0, "success", gin.H{
		"users": data,
	})
}
